// Player.cpp //
#include "Player.h"

#include "Sound.h"
#include "Enemy.h"
#include "PlBullet.h"

//the actual player info, player class deals with construction
// and with handling user events, player ship deals with interaction
//on screen

#define PLAYER_COUNT 1

SDLKey StartKey = {SDLK_SPACE};
// these are all made of two keys to allow user choice in control scheme
SDLKey UpKey[2]    = {SDLK_w,     SDLK_UP};
SDLKey DownKey[2]  = {SDLK_s,     SDLK_DOWN};
SDLKey LeftKey[2]  = {SDLK_a,     SDLK_LEFT};
SDLKey RightKey[2] = {SDLK_d,     SDLK_RIGHT};
SDLKey FireKey[2]  = {SDLK_j,     SDLK_RCTRL};
SDLKey Fire0[2]  = {SDLK_0,     SDLK_KP0};   
SDLKey Fire1[2]  = {SDLK_1,     SDLK_KP1};   
SDLKey Fire2[2]  = {SDLK_2,     SDLK_KP2};   
SDLKey Fire3[2]  = {SDLK_3,     SDLK_KP3};   
SDLKey Fire4[2]  = {SDLK_4,     SDLK_KP4};   
SDLKey Fire5[2]  = {SDLK_5,     SDLK_KP5};   
SDLKey Fire6[2]  = {SDLK_6,     SDLK_KP6};   
SDLKey Fire7[2]  = {SDLK_7,     SDLK_KP7};   
SDLKey Fire8[2]  = {SDLK_8,     SDLK_KP8};   
SDLKey Fire9[2]  = {SDLK_9,     SDLK_KP9};   

Player* Player::Players = NULL;

int BulletNum = 0;

Player::Player() // create player 
{
	numValue = 0; // not useful but needed to prevent collision bug
	ship = NULL;
	lives = PlayerLives;
	score = 0;
}

void Player::AddPlayers()
{
    Players = new Player[PLAYER_COUNT];
	Players[0] = Player();
}

void Player::RemovePlayers()
{
	delete [] Players;
	Players = NULL;
}

void Player::SpawnPlayer(int index)
{
	if(Players[0].ship == NULL && Players[0].lives > 0)
	{
        char player[7];
		sprintf(player, "Player1");
	    Players[0].ship = new PlayerShip(player);
	}
}

void Player::DestroyPlayer(PlayerShip* ship)
{
	if(Players[0].ship == ship)
	{
	    Players[0].ship = NULL;
	   	Players[0].lives--;
	}
}

void Player::ProcessInput(SDL_Event& event)
{
		Players[0].ProcessInput(event, 0);
}

// gets all user input, allows for two control schemes
void Player::ProcessInput(SDL_Event& event, int index)
{
	if(ship == NULL)
	{
		if(event.type == SDL_KEYDOWN)
			if(event.key.keysym.sym == StartKey)
               	SpawnPlayer(0);
	}
	else
	{
        if(event.type == SDL_KEYDOWN)
        {
			if(event.key.keysym.sym == UpKey[0] || event.key.keysym.sym == UpKey[1])
				ship->movingUp = true;
			if(event.key.keysym.sym == DownKey[0] || event.key.keysym.sym == DownKey[1])
				ship->movingDown = true;
			if(event.key.keysym.sym == LeftKey[0] || event.key.keysym.sym == LeftKey[1])
				ship->movingLeft = true;
			if(event.key.keysym.sym == RightKey[0] || event.key.keysym.sym == RightKey[1])
				ship->movingRight = true;
			if(event.key.keysym.sym == Fire0[0] || event.key.keysym.sym == Fire0[1])
			{
				ship->shooting = true;
				BulletNum = 0;
			}
			if(event.key.keysym.sym == Fire1[0] || event.key.keysym.sym == Fire1[1])
			{
				ship->shooting = true;
				BulletNum = 1;
			}
			if(event.key.keysym.sym == Fire2[0] || event.key.keysym.sym == Fire2[1])
			{
				ship->shooting = true;
				BulletNum = 2;
			}
			if(event.key.keysym.sym == Fire3[0] || event.key.keysym.sym == Fire3[1])
			{
				ship->shooting = true;
				BulletNum = 3;
			}
			if(event.key.keysym.sym == Fire4[0] || event.key.keysym.sym == Fire4[1])
			{
				ship->shooting = true;
				BulletNum = 4;
			}
			if(event.key.keysym.sym == Fire5[0] || event.key.keysym.sym == Fire5[1])
			{
				ship->shooting = true;
				BulletNum = 5;
			}
			if(event.key.keysym.sym == Fire6[0] || event.key.keysym.sym == Fire6[1])
			{
				ship->shooting = true;
				BulletNum = 6;
			}
			if(event.key.keysym.sym == Fire7[0] || event.key.keysym.sym == Fire7[1])
			{
				ship->shooting = true;
				BulletNum = 7;
			}
			if(event.key.keysym.sym == Fire8[0] || event.key.keysym.sym == Fire8[1])
			{
				ship->shooting = true;
				BulletNum = 8;
			}
			if(event.key.keysym.sym == Fire9[0] || event.key.keysym.sym == Fire9[1])
			{
				ship->shooting = true;
				BulletNum = 9;
			}
		}
		else if(event.type == SDL_KEYUP)
        {
			if(event.key.keysym.sym == UpKey[0] || event.key.keysym.sym == UpKey[1])
				ship->movingUp = false;
			if(event.key.keysym.sym == DownKey[0] || event.key.keysym.sym == DownKey[1])
				ship->movingDown = false;
			if(event.key.keysym.sym == LeftKey[0] || event.key.keysym.sym == LeftKey[1])
				ship->movingLeft = false;
			if(event.key.keysym.sym == RightKey[0] || event.key.keysym.sym == RightKey[1])
				ship->movingRight = false;
			if(event.key.keysym.sym == Fire0[0] || event.key.keysym.sym == Fire0[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire1[0] || event.key.keysym.sym == Fire1[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire2[0] || event.key.keysym.sym == Fire2[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire3[0] || event.key.keysym.sym == Fire3[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire4[0] || event.key.keysym.sym == Fire4[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire5[0] || event.key.keysym.sym == Fire5[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire6[0] || event.key.keysym.sym == Fire6[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire7[0] || event.key.keysym.sym == Fire7[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire8[0] || event.key.keysym.sym == Fire8[1])
			{
				ship->shooting = false;
			}
			if(event.key.keysym.sym == Fire9[0] || event.key.keysym.sym == Fire9[1])
			{
				ship->shooting = false;
			}
		}
	}
}

void Player::AwardPoints(int amount)
{
		Players[0].score += amount;
}

// PlayerShip.cpp //

vector<GameObject*> PlayerShip::PlayerShipList; // again, remnant of multiplayer

PlayerShip::PlayerShip(string keyname) : GameObject(keyname) // constructor
{
	position = Vector(ScreenWidth / 2, ScreenHeight - sprite->texture->tileHeight - PlayerSpawnOffset);
	speed = PlayerSpeed;
	health = PlayerHealth;
	explosionKeyname = "Explosion";
	weaponType = WEAPON_BULLET;
	CollisionList = &Enemy::EnemyList;

	movingUp = false;
	movingDown = false;
	movingLeft = false;
	movingRight = false;
	shooting = false;
	
	lastFired = 0.0f;
	
	PlayerShipList.push_back(this);
}
//moves player based on input
void PlayerShip::Update(float deltaTime) 
{
	numValue = 0;
	direction = Vector(0, 0);

	if(movingUp)
        direction.Y -= 1;
	if(movingDown)
        direction.Y += 1;
	if(movingLeft)
        direction.X -= 1;
	if(movingRight)
        direction.X += 1;

	lastFired += deltaTime;
	FireBullet();

	GameObject::Update(deltaTime);

	// keep the player on teh screen 

	if(position.X < sprite->origin.X)
	    position.X = sprite->origin.X;
	else if(position.X > ScreenWidth - sprite->origin.X)
	    position.X = ScreenWidth - sprite->origin.X;

    if(position.Y < sprite->origin.Y)
	    position.Y = sprite->origin.Y;
	else if(position.Y > ScreenHeight - sprite->origin.Y)
	    position.Y = ScreenHeight - sprite->origin.Y;
}

void PlayerShip::RemoveKilled()
{
    for(vector<GameObject*>::iterator Iter = PlayerShipList.begin(); Iter != PlayerShipList.end(); Iter += 0)
        if((*Iter)->dead)
		    Iter = PlayerShipList.erase(Iter);
		else
		    ++Iter;
}

void PlayerShip::RemoveAll()
{
    for(vector<GameObject*>::iterator Iter = PlayerShipList.begin(); Iter != PlayerShipList.end(); Iter += 0)
		Iter = PlayerShipList.erase(Iter);
}

void PlayerShip::Remove()
{
    Player::DestroyPlayer(this);

	GameObject::Remove();
}

void PlayerShip::Collide(GameObject* object)
{
	int objectHealth = object->health;
	object->TakeDamage(this->health);
	this->TakeDamage(objectHealth);
}

void PlayerShip::FireBullet() // fires numbered bullets
{
	if(shooting)
	{
		if(weaponType == WEAPON_BULLET)
		{
			if(lastFired > PlayerFireDelay)
			{
				//uses the player bullet class
				Projectile* projectile = new PlBullet("Bullet"); 
				projectile->numValue = BulletNum;
			    projectile->position = position - Vector(0.0f, 20.0f);
			    projectile->direction = Vector(0, -1);
			    projectile->CollisionList = &Enemy::EnemyList;
			    lastFired = 0.0f;
			}
		}
	}
}
